package server;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class Main {
    public static void main(String[] args) {
        if ( args.length == 0 ) {
            System.err.println("usage: java server.Main port#");
            System.exit(1);
        }
        int index = 0;
        int port = Integer.parseInt(args[index++]);
        //创建服务器对象
        Greeting greeting = new GreetingObject();
        try {
            //从 RMI 运行时获取服务器对象的存根
            //此处端口为0表示会使用随机端口号。但是在有防火墙的情况下，可能随机端口号会被拦截，所以可以设置一个固定值，比如8000什么的，即改成
            //(Greeting)UnicastRemoteObject.exportObject(greeting, 8000)

            Greeting stub = (Greeting) UnicastRemoteObject.exportObject(greeting, 0);//
            //交给 RMI 注册中心以绑定到指定的命名服务
            String name = "Greeting";
            Registry registry = LocateRegistry.getRegistry(port);
            registry.rebind(name, stub);
            System.out.println("Greeting bound to \"" + name + "\"");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
